<?php
//
// +---------------------------------------------------------------------------------+
// | Package: EezeeSite Core                                                         |
// +---------------------------------------------------------------------------------+
// | Copyright (C) 2006  EezeeSite Development Team                                  |
// +---------------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or                   |
// | modify it under the terms of the GNU General Public License                     |
// | as published by the Free Software Foundation; either version 2                  |
// | of the License, or (at your option) any later version.                          |
// |                                                                                 |
// | This program is distributed in the hope that it will be useful,                 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                  |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   |
// | GNU General Public License for more details.                                    |
// |                                                                                 |
// | You should have received a copy of the GNU General Public License               |
// | along with this program; if not, write to the Free Software                     |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. |
// +---------------------------------------------------------------------------------+
// | Author: Mayukh De <mayukhde@gmail.com>                                          |
// +---------------------------------------------------------------------------------+
//

class ESAuthenticationModeAJAX extends ESAuthentication
{   
    function authStatus()
    {
        global $ESController;
        if (!$this->auth->isLoggedIn()) {
            $msg = $ESController->getLink(sprintf($GLOBALS['ES']['LANG']['AUTH']['STATUS']['UNLOGGED'][0], ES_ICO_URL), null, null, array('ONCLICK' => 'showLoginBox();'), 'login');
            $msg.= ' | ';
            $msg.= $ESController->getLink(sprintf($GLOBALS['ES']['LANG']['AUTH']['STATUS']['UNLOGGED'][1], ES_ICO_URL), 'REGISTER');
            $this->AJAXResponse->assignAttr('authStatusBox', 'innerHTML', $msg);
        } else {
            $msg = $this->getAuthBoxMessage();
            $this->AJAXResponse->assignAttr('authStatusBox', 'innerHTML', $msg);
        }
        return $this->AJAXResponse;
    }
    
    function authenticated()
    {
        global $ESController;
        if ($this->auth->isLoggedIn()) {
            $this->AJAXResponse->insertAlert("Since you are logged in you may not use the user registration form again.\nPlease visit your account management page.");
            $this->AJAXResponse->insertScript("window.location = '".ES_BASE_URL."?mode=my';");
        } else {
            //$this->AJAXResponse->insertScript("Effect.toggle('registercontainer', 'appear');");
            //$this->AJAXResponse->assignAttr('floatingBoxHeader', 'innerHTML', '<b>Register</b>');
            $this->AJAXResponse->insertScript("showFloat();");
            $this->AJAXResponse->insertScript("showFloatingContent('Sign Up For a Free Account', 'registerbox');");
        }
        return $this->AJAXResponse;
    }
    
    function logIn($input)
    {
        $input = get_object_vars($input);
        $this->auth->init();
        if ($input['username'] && $input['password']) {
            $this->auth->login($input['username'], $input['password']);
            $this->updateAuthVals();
        }
        if (!is_null($this->authFailMessage())) {
            $this->AJAXResponse->insertAlert($this->authFailMessage());
        } else {
            $this->AJAXResponse->insertScript("hideFloat();");
            $msg = $this->getAuthBoxMessage();
            $this->AJAXResponse->assignAttr('authStatusBox', 'innerHTML', $msg);
        }
        return $this->AJAXResponse;
    }
    
    function logOut()
    {
        $this->auth->logout();
        $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['AUTH']['OK']['LOGOUT']);
        $this->authStatus();
        return $this->AJAXResponse;
    }
    
    function passwordReminder($input)
    {
        $input = get_object_vars($input);
        if (eregi('@', $input['user'])) {
            $data['email'] = $input['user'];
        } else {
            $data['handle'] = $input['user'];
        }
        $userDetails = $this->getUserDetails($data);
        if (!$userDetails) {
            $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['AUTH']['PASSWORD']['FAIL']);
        } else {
            $temp = $this->newPassword($userDetails);
            $this->AJAXResponse->insertAlert($temp);
            $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['AUTH']['PASSWORD']['OK']);
            $this->AJAXResponse->insertScript("hideFloat();");
        }
        return $this->AJAXResponse;
    }
    
    function getAuthBoxMessage()
    {
        global $ESController;
        $userDetails = $this->getUserDetails();
        $msg = sprintf($GLOBALS['ES']['LANG']['AUTH']['STATUS']['LOGGEDIN'][0], ES_ICO_URL, $userDetails['handle']);
        $msg.= ' | ';
        $msg.= $ESController->getLink(sprintf($GLOBALS['ES']['LANG']['AUTH']['STATUS']['LOGGEDIN'][1], ES_ICO_URL), 'MYHOME');
        $msg.= ' | ';
        $msg.= $ESController->getLink(sprintf($GLOBALS['ES']['LANG']['AUTH']['STATUS']['LOGGEDIN'][2], ES_ICO_URL), null, null, array('ONCLICK' => 'logOut();'), 'logout');
        $msg.= ($userDetails['user_group'] == 1) ? ' | '.$ESController->getLink('<img src="'.ES_ICO_URL.'cog.png />admin', 'ADMINHOME', array(ES_FILE_EXTN)) : '';
        return $msg;
    }
}

?>